<?php

// +----------------------------------------------------------------------
// | 用户资格证数据表
// +----------------------------------------------------------------------

namespace common\model;

use think\model\RelationModel;

class UserCertModel extends RelationModel
{

    // 自动验证
    protected $_validate = [
        ['user_cert_category_id', '', '', self::MUST_VALIDATE, 'require', self::MODEL_INSERT],//必须字段
        ['step_sku_ids', '', '', self::MUST_VALIDATE, 'require', self::MODEL_INSERT],//必须字段
        ['step_sku_ids', '0,16777215', '', self::EXISTS_VALIDATE, 'length'],
        ['base_man_hour', '0,36', '', self::EXISTS_VALIDATE, 'length'],
        ['level', '', '', self::MUST_VALIDATE, 'require', self::MODEL_INSERT],//必须字段
        ['level_category', '', '', self::MUST_VALIDATE, 'require', self::MODEL_INSERT],//必须字段
        ['level_category', ['s', 'a', 'b', 'c'], '', self::EXISTS_VALIDATE, 'in'],
//        ['user_cert_category_id,level,level_category', '', '', self::MUST_VALIDATE, 'unique'],
        ['user_cert_category_id,level', '', '', self::MUST_VALIDATE, 'unique']
    ];

    // 自动完成
    protected $_auto = [
        ['created', 'time', self::MODEL_INSERT, 'function'],
        ['step_attribute_config', 'json_encode', self::EXISTS_VALIDATE, 'function']
    ];

    /**
     * 获取器：自定义字段
     * @param $value
     * @param $data
     * @return array|mixed
     */
    public function getStepAttributeConfigAttr($value, $data)
    {
        if (!empty($value)) {
            return json_decode($value, true);
        }

        return [];
    }

    /**
     * 获取器：创建时间
     * @param $value
     * @param $data
     * @return array|mixed
     */
//    public function getCreatedAttr($value, $data)
//    {
//        if (!empty($value)) {
//            return get_format_date($value, 1);
//        }
//        return "";
//    }
}
